package com.appview;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.appdatebase.*;

public class QueryInfo extends JFrame implements ActionListener{
	private JPanel jPanel1, jPanel2, jPanel3, jPanel3_1, jPanel3_2, jPanel4, jPanel5, jPanel6, jPanel7, jPanel8;
	private JLabel jLabel1, jLabel2, jLabel3,jLabel4,jLabel5,jLabel6,jLabel7,jLabel8;
	private JButton jButton1, jButton2, jButton3;
	private JTextField jField1, jField2,jField3,jField4,jField5,jField6,jField7,jField8;

	public QueryInfo(HashMap<String, String> infomMap, int type){
		//设置文字
		jLabel1 = new JLabel("学号:                  ");
		jLabel2 = new JLabel("姓名:                  ");
		jLabel3 = new JLabel("性别:                  ");
		jLabel4 = new JLabel("民族:                  ");
		jLabel5 = new JLabel("出生日期:         ");
		jLabel6 = new JLabel("政治面貌:         ");
		jLabel7 = new JLabel("学院:                  ");
		jLabel8 = new JLabel("年级专业班级:");

		//设置文本框
		jField1 = new JTextField(10);
		jField2 = new JTextField(10);
		jField3 = new JTextField(5);
		jField4 = new JTextField(5);
		jField5 = new JTextField(13);
		jField6 = new JTextField(13);
		jField7 = new JTextField(13);
		jField8 = new JTextField(13);

		if (infomMap != null) {
			jField1.setText(infomMap.get("stuId"));
			jField2.setText(infomMap.get("stuName"));
			jField3.setText(infomMap.get("stuSex"));
			jField4.setText(infomMap.get("stuFrom"));
			jField5.setText(infomMap.get("stuBir"));
			jField6.setText(infomMap.get("stuStatus"));
			jField7.setText(infomMap.get("stuColloge"));
			jField8.setText(infomMap.get("stuClass"));

		}

		jButton1 = new JButton("修改");
		jButton2 = new JButton("取消");

		jButton1.addActionListener(this);
		jButton2.addActionListener(this);

		if (type == 0) {

			jField1.setEditable(false);
			jField2.setEditable(false);
			jField3.setEditable(false);
			jField4.setEditable(false);
			jField5.setEditable(false);
			jField6.setEditable(false);
			jField7.setEditable(false);
			jField8.setEditable(false);

			jButton1.setVisible(false);
			jButton2.setVisible(false);
		}else if (type == 1) {
			jField1.setEditable(false);
			jField2.setEditable(false);
		}
		//添加组件
		jPanel1 = new JPanel();
		jPanel2 = new JPanel();
		jPanel3 = new JPanel();
		jPanel3_1 = new JPanel();
		jPanel3_2 = new JPanel();
		jPanel4 = new JPanel();
		jPanel5 = new JPanel();
		jPanel6 = new JPanel();
		jPanel7 = new JPanel();
		jPanel8 = new JPanel();

		jPanel1.add(jLabel1, BorderLayout.WEST);
		jPanel1.add(jField1, BorderLayout.CENTER);

		jPanel2.add(jLabel2, BorderLayout.WEST);
		jPanel2.add(jField2, BorderLayout.CENTER);

		jPanel3_1.add(jLabel3);
		jPanel3_1.add(jField3);
		jPanel3_2.add(jLabel4);
		jPanel3_2.add(jField4);

		jPanel3.add(jPanel3_1, BorderLayout.WEST);
		jPanel3.add(jPanel3_2, BorderLayout.EAST);

		jPanel4.add(jLabel5, BorderLayout.WEST);
		jPanel4.add(jField5, BorderLayout.CENTER);

		jPanel5.add(jLabel6, BorderLayout.WEST);
		jPanel5.add(jField6, BorderLayout.CENTER);

		jPanel6.add(jLabel7, BorderLayout.WEST);
		jPanel6.add(jField7, BorderLayout.CENTER);

		jPanel7.add(jLabel8, BorderLayout.WEST);
		jPanel7.add(jField8, BorderLayout.CENTER);

		jPanel8.add(jButton1);
		jPanel8.add(jButton2);

		jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel3.setLayout(new GridLayout(1, 2));
		jPanel3_1.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel3_2.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel4.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel5.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel6.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel7.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel8.setLayout(new FlowLayout(FlowLayout.CENTER));

		this.add(jPanel1);
		this.add(jPanel2);
		this.add(jPanel3);
		this.add(jPanel4);
		this.add(jPanel5);
		this.add(jPanel6);
		this.add(jPanel7);
		this.add(jPanel8);
		this.setLayout(new GridLayout(8, 1, 1, 1));
		this.setTitle("学生信息");
		this.setSize(318,340);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		HashMap<String, String> infoMap = getHashMap();
		if (e.getSource() == jButton1) {
			new ModifyUserInfo(infoMap);
			QueryInfo.this.dispose();
		} else {
			QueryInfo.this.dispose();
		}
	}

	private HashMap<String, String> getHashMap() {
		HashMap<String, String> hashMap = new HashMap<>();
		hashMap.put("stuId", jField1.getText());
		hashMap.put("stuName", jField2.getText());
		hashMap.put("stuSex", jField3.getText());
		hashMap.put("stuBir", jField4.getText());
		hashMap.put("stuFrom", jField5.getText());
		hashMap.put("stuStatus", jField6.getText());
		hashMap.put("stuColloge", jField7.getText());
		hashMap.put("stuClass", jField8.getText());

		return hashMap;
	}

}
